/*
 * Copyright 2009 Johnson Consulting Services
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * 
 */
package com.johnsoncs.gwt.widgets.samples.client;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.johnsoncs.gwt.widgets.samples.client.focus.FocusGroupTest;

/**
 * @author wilj
 *
 */
public class FocusGroupsDemo extends Composite {
	private final VerticalPanel panel = new VerticalPanel();
	private final ListBox messages = new ListBox();

	public FocusGroupsDemo() {
		panel.add(createGroup("1"));
		panel.add(createGroup("2"));
		panel.add(createGroup("3"));
		panel.add(createGroup("4"));
		messages.setVisibleItemCount(10);
		panel.add(messages);
		super.initWidget(panel);
	}

	private FocusGroupTest createGroup(final String name) {
		final FocusGroupTest result = new FocusGroupTest();
		result.addFocusHandler(new FocusHandler() {
			@Override
			public void onFocus(final FocusEvent event) {
				messages.addItem(name + " received focus");
				messages.setSelectedIndex(messages.getItemCount() - 1);
			}
		});
		result.addBlurHandler(new BlurHandler() {
			@Override
			public void onBlur(final BlurEvent event) {
				messages.addItem(name + " lost focus");
				messages.setSelectedIndex(messages.getItemCount() - 1);
			}
		});
		return result;
	}

}
